package LeetCode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class no819 {
    class Solution {
        public String mostCommonWord(String paragraph, String[] banned) {
            //String split[] = paragraph.toLowerCase().split("[^a-z]+");
            StringBuilder sb=new StringBuilder();
            HashMap<String,Integer> map=new HashMap<>();
            HashSet<String> ban=new HashSet<>();

            for(String str:banned){
                ban.add(str);
            }

            char[] chars=paragraph.toLowerCase().toCharArray();
            for(char ch:chars){
                if(ch>='a'&&ch<='z'){
                    sb.append(ch);
                }else{
                    String key= sb.toString();

                    if(!key.isEmpty()&&!ban.contains(key)){
                        // 如果key存在就加一，如果不存在就设为1
                        map.put(key, map.getOrDefault(key, 0) + 1);
                    }
                    sb.setLength(0);
                }

            }

            if(sb.length()>0){
                String key= sb.toString();
                if(!ban.contains(key)){
                    // 如果key存在就加一，如果不存在就设为1
                    map.put(key, map.getOrDefault(key, 0) + 1);
                }
            }


            Map.Entry <String,Integer> Max=null;

            for(Map.Entry<String,Integer> entry:map.entrySet()){
                if(Max==null || entry.getValue()>Max.getValue()){
                    Max=entry;
                }
            }

            return Max.getKey();

        }
    }
}
